package com.tjmc.nsia.core.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.tjmc.nsia.core.service.WxChatService;
import com.tjmc.nsia.core.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;

/**
 * @author Luc
 * @version 1.0
 * @date 2024-07-03 21:25
 */
@Service
public class WxChatServiceImpl implements WxChatService {

    private static final Logger logger = LoggerFactory.getLogger(WxChatServiceImpl.class);

    @Value("${wx.appid}")
    private String appid;

    @Value("${wx.secret}")
    private String secret;


    @Override
    public JSONObject jscode2session(String code) {
        if(StringUtils.hasText(code)) {
            String response = HttpUtils.sendPlainPost("https://api.weixin.qq.com/sns/jscode2session", new HashMap<String, Object>(){{
                put("appid", appid);
                put("secret", secret);
                put("js_code", code);
                put("grant_type", "authorization_code");
            }});
            logger.info("jscode2session: code=" + code + ", response=" + response);
            return JSONObject.parseObject(response);
        }
        return null;
    }
}
